/****************************************************
	文件：UIScriptGenerator.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/3/24 9:50:53
	功能：UI 脚本生成工具
*****************************************************/

using System.Collections.Generic;
using System.Text;
using HTFW.Logger;
using HTFW.Utility.Editor;
using HTFW.Utility.Runtime;
using UnityEditor;
using UnityEngine;

namespace HTFW.UI
{
    public class UIScriptGenerator
    {
        string mUIName;
        StringBuilder mSB;

        public UIScriptGenerator(string name)
        {
            if (!name.Contains("View"))
                HLog.Error($"UI 命名不规范，{name} 必须以 View 结尾");

            mUIName = name;
            mSB = new StringBuilder();
        }

        public void Generate(UIGeneratorType type)
        {
            string userName = "HuskyT";
            string mailAddress = "1005240602@qq.com";
            string namespaceStr = "HTFW.UI";
            string className = GetClassName(type);
            mSB.Clear();
            string outputPath = GetOutputPath(type);
            HLog.Debug($"outputPath : {outputPath}");
            RuntimeUtil.CheckOrCreateFolder(GetOutputFolderPath(type));

            //Head
            EditorUtil.EditHeadInfo(ref mSB, className, userName, mailAddress);
            //Using
            EditorUtil.EditUsing(ref mSB, "System");
            EditorUtil.EditUsing(ref mSB, "UnityEngine");
            if (namespaceStr != "HTFW.UI")
                EditorUtil.EditUsing(ref mSB, "HTFW.UI");
            mSB.AppendLine();
            //命名空间
            EditorUtil.EditNameSpace(ref mSB, namespaceStr);
            mSB.AppendLine("{"); //namespace
            //类名
            EditorUtil.EditClassName(ref mSB, className, GetInherit(type));
            mSB.AppendLine("\t{"); //class
            //方法
            EditFunction(ref mSB, type);

            mSB.AppendLine("\t}"); //class
            mSB.AppendLine("}"); //namespace

            RuntimeUtil.WriteStringToFile(mSB.ToString(), outputPath, Encoding.UTF8);

            AssetDatabase.Refresh();
        }

        string GetClassName(UIGeneratorType type)
        {
            string firstName = mUIName.Replace("View", string.Empty);
            switch (type)
            {
                case UIGeneratorType.None:
                    return string.Empty;
                case UIGeneratorType.Model:
                    return $"{firstName}Model";
                case UIGeneratorType.View:
                    return $"{firstName}View";
                case UIGeneratorType.Controller:
                    return $"{firstName}Controller";
                default:
                    HLog.Error($"GetClassName error! 尚未定义的 UIGeneratorType : {type}");
                    return string.Empty;
            }
        }

        string GetOutputFolderPath(UIGeneratorType type)
        {
            string rootPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/"));
            //rootPath = "E:/Work/Gitee/HTFW";
            return $"{rootPath}/{UIScriptGeneratorDefine.UI_SCRIPT_GENERATOR_FOLDER_PATH}{type}/";
        }

        string GetOutputPath(UIGeneratorType type)
        {
            return $"{GetOutputFolderPath(type)}{GetClassName(type)}.cs";
        }

        string GetInherit(UIGeneratorType type)
        {
            switch (type)
            {
                case UIGeneratorType.None:
                    return string.Empty;
                case UIGeneratorType.Model:
                    return "ModelBase";
                case UIGeneratorType.View:
                    return "ViewBase";
                case UIGeneratorType.Controller:
                    return
                        $"ControllerBase<{GetClassName(UIGeneratorType.View)}, {GetClassName(UIGeneratorType.Model)}>";
                default:
                    HLog.Error($"GetInherit error! 尚未定义的 UIGeneratorType : {type}");
                    return string.Empty;
            }
        }

        void EditFunction(ref StringBuilder sb, UIGeneratorType type)
        {
            switch (type)
            {
                case UIGeneratorType.None:
                    break;
                case UIGeneratorType.Model:
                    EditModelFunction(ref sb);
                    break;
                case UIGeneratorType.View:
                    EditViewFunction(ref sb);
                    break;
                case UIGeneratorType.Controller:
                    EditControllerFunction(ref sb);
                    break;
                default:
                    HLog.Error($"EditFunction error! 尚未定义的 UIGeneratorType : {type}");
                    break;
            }
        }

        void EditModelFunction(ref StringBuilder sb)
        {
            EditorUtil.EditFunction(ref sb, "public", false, true, "", "Dispose", null, "", "");
        }

        void EditViewFunction(ref StringBuilder sb)
        {
            EditorUtil.EditFunction(ref sb, "public", false, true, "", "SetupUI", null, "", "base.SetupUI();");
            EditorUtil.EditFunction(ref sb, "protected", false, true, "", "OnInit", null, "", "");
        }

        void EditControllerFunction(ref StringBuilder sb)
        {
            EditorUtil.EditFunction(ref sb, "public", false, false, "null", GetClassName(UIGeneratorType.Controller),
                new List<string>()
                    {GetClassName(UIGeneratorType.View) + " view", GetClassName(UIGeneratorType.Model) + " model"},
                ": base(view, model)", "");

            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("\t\t//------------------------------ Events ------------------------------//");
            sb.AppendLine();
            sb.AppendLine("\t\t#region Events");
            sb.AppendLine();
            EditorUtil.EditFunction(ref sb, "protected", false, true, "", "RegisterEvents", null, "", "");
            sb.AppendLine();
            sb.AppendLine("\t\t#endregion");
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("\t\t//------------------------------ Model ------------------------------//");
            sb.AppendLine();
            sb.AppendLine("\t\t#region Model");
            sb.AppendLine();
            EditorUtil.EditFunction(ref sb, "protected", false, true, "", "InitModel", null, "", "");
            sb.AppendLine();
            sb.AppendLine("\t\t#endregion");
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("\t\t//------------------------------ View ------------------------------//");
            sb.AppendLine();
            sb.AppendLine("\t\t#region View");
            sb.AppendLine();
            EditorUtil.EditFunction(ref sb, "protected", false, true, "", "InitView", null, "", "");
            sb.AppendLine();
            sb.AppendLine("\t\t#endregion");
        }
    }
}